package com.punai.monthanalysis.bo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import cn.hutool.core.util.StrUtil;
import com.punai.common.utils.NumberUtil;
import com.punai.common.utils.bean.BeanUtils;
import com.punai.externalccb.bo.YdCbfeeiteminfoStandardBo;
import com.punai.monthanalysis.domain.ProYdcbfxZyfbgcdbfxb;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class ProYdcbfxZyfbgcdbfxbBo extends ProYdcbfxZyfbgcdbfxb {

    private Integer rowspan = 1;
    private Integer colspan = 1;
    private List<ProYdcbfxZyfbgcdbfxbBo> children = new ArrayList<>();

    public Integer getRowspan() {
        return rowspan;
    }

    public void setRowspan(Integer rowspan) {
        this.rowspan = rowspan;
    }

    public Integer getColspan() {
        return colspan;
    }

    public void setColspan(Integer colspan) {
        this.colspan = colspan;
    }

    public List<ProYdcbfxZyfbgcdbfxbBo> getChildren() {
        return children;
    }

    public void setChildren(List<ProYdcbfxZyfbgcdbfxbBo> children) {
        this.children = children;
    }

    public void clearColRow() {
        this.setRowspan(0);
        this.setColspan(0);
    }

    public static List<ProYdcbfxZyfbgcdbfxbBo> convertStandard(List<YdCbfeeiteminfoStandardBo> list){
        List<ProYdcbfxZyfbgcdbfxbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        for (YdCbfeeiteminfoStandardBo standard : list) {
            ProYdcbfxZyfbgcdbfxbBo bo = new ProYdcbfxZyfbgcdbfxbBo();
            bo.setId(standard.getId());
            bo.setProId(standard.getProId());
            bo.setQc(standard.getYdDate());//期次Id
            bo.setCategory(standard.getCategoryName()==null?"":standard.getCategoryName());//分类
            bo.setItemName(standard.getName());
            bo.setUnit(standard.getUnit());

            bo.setYsQuantity(standard.getMonthAmount());
            bo.setYsAvgPrice(standard.getMonthPrice());
            bo.setYsPrice(standard.getMonthCzTotal());

            bo.setSjQuantity(standard.getActualCostAmout());
            bo.setSjAvgPrice(standard.getActualCostNoTaxPrice());
            bo.setSjPrice(standard.getActualCostNoTaxTotal());

            BigDecimal ysAmount = standard.getMonthAmount();
            BigDecimal ysTotal = standard.getMonthCzTotal();
            BigDecimal jcQuantity = standard.getDifferenceOfCzsjAmount();
            BigDecimal quantityJcl = NumberUtil.divideNew(jcQuantity, 2, ysAmount);
            BigDecimal price = standard.getKlDifferenceOfCzsjTotal();
            BigDecimal priceJcl = NumberUtil.divideNew(price, 2, ysTotal);
            bo.setJcQuantity(jcQuantity);
            bo.setQuantityJcl(quantityJcl);
            bo.setPrice(price);
            bo.setPriceJcl(priceJcl);
            result.add(bo);
        }
        Map<String, List<ProYdcbfxZyfbgcdbfxbBo>> listMap = result.stream().filter(v -> v.getCategory() != null).collect(Collectors.groupingBy(ProYdcbfxZyfbgcdbfxbBo::getCategory, Collectors.toList()));
        return  convert(listMap);
    }

    public static List<ProYdcbfxZyfbgcdbfxbBo> convert(Map<String, List<ProYdcbfxZyfbgcdbfxbBo>> listMap) {
        List<ProYdcbfxZyfbgcdbfxbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(listMap)) {
            return result;
        }
        for (Map.Entry<String, List<ProYdcbfxZyfbgcdbfxbBo>> listEntry : listMap.entrySet()) {
            List<ProYdcbfxZyfbgcdbfxbBo> entryValues = listEntry.getValue();
            for (int i = 0; i < entryValues.size(); i++) {
                ProYdcbfxZyfbgcdbfxbBo bo = entryValues.get(i);
                if (i == 0){
                    bo.setRowspan(entryValues.size());
                }else {
                    bo.setCategory(null);
                    bo.clearColRow();
                }
                result.add(bo);
            }
        }
        return result;
    }

    public static List<ProYdcbfxZyfbgcdbfxbBo> convert(List<ProYdcbfxZyfbgcdbfxb> inList) {
        List<ProYdcbfxZyfbgcdbfxbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(inList)) {
            return result;
        }

        ListValueMap<String,ProYdcbfxZyfbgcdbfxb> levelMap = new ListValueMap<>();
        for (ProYdcbfxZyfbgcdbfxb gcjdkhcldbfxb : inList) {
            levelMap.putValue(StrUtil.blankToDefault(gcjdkhcldbfxb.getPid(),"0"),gcjdkhcldbfxb);
        }
        List<ProYdcbfxZyfbgcdbfxb> roots = levelMap.get("0");
        roots.sort(Comparator.comparing(ProYdcbfxZyfbgcdbfxb::getXh));


        ProYdcbfxZyfbgcdbfxbBo hjBo = new ProYdcbfxZyfbgcdbfxbBo();
        // 只拆解两层
        for (ProYdcbfxZyfbgcdbfxb root : roots) {
            String id = root.getId();
            ProYdcbfxZyfbgcdbfxbBo convert = convert(root);
            List<ProYdcbfxZyfbgcdbfxb> children = levelMap.get(id);
            if (CollUtil.isNotEmpty(children)) {
                convert.setRowspan(children.size() + 1);

                result.add(convert);
                children.sort(Comparator.comparing(ProYdcbfxZyfbgcdbfxb::getXh));

                for (ProYdcbfxZyfbgcdbfxb child : children) {
                    ProYdcbfxZyfbgcdbfxbBo childrenBo = convert(child);
                    childrenBo.clearColRow();
                    result.add(childrenBo);
                }
            } else {
                result.add(convert);
            }
        }
        hjBo.setItemName("合计");
        hjBo.setYsPrice(roots.stream().map(ProYdcbfxZyfbgcdbfxb::getYsPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add));
        hjBo.setSjPrice(roots.stream().map(ProYdcbfxZyfbgcdbfxb::getSjPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add));
        hjBo.setPrice(roots.stream().map(ProYdcbfxZyfbgcdbfxb::getPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add));
        hjBo.setPriceJcl(NumberUtil.divide(hjBo.getPrice(),3,hjBo.getYsPrice()));
        result.add(hjBo);
        return result;
    }

    public static ProYdcbfxZyfbgcdbfxbBo convert(ProYdcbfxZyfbgcdbfxb in) {
        ProYdcbfxZyfbgcdbfxbBo out = new ProYdcbfxZyfbgcdbfxbBo();
        BeanUtils.copyBeanProp(out,in);
        return out;
    }


}
